package com.mifengkong.frwidget.titlebar;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frwidget.R;

/**
 * Created by jack on 17/5/18
 */

public class WebViewTitleBarUIComponent extends RelativeLayout {

    public WebViewTitleBarUIComponent(Context context) {
        super(context);
    }

    public WebViewTitleBarUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WebViewTitleBarUIComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TextView mTvTitleBarLeft;
    private TextView mTvTitleBarTitle;
    private TextView mTvTitleBarRight;
    private ImageView mIvTitleBarRight;
    private ImageView mIvTitleBarLeftToRefresh;
    private View mBottomView;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTvTitleBarLeft = findViewById(R.id.lwt_tv_titlebar_left);
        mTvTitleBarTitle = findViewById(R.id.lwt_tv_titlebar_title);
        mTvTitleBarRight = findViewById(R.id.lwt_tv_titlebar_right);
        mIvTitleBarRight = findViewById(R.id.lwt_iv_titlebar_right);
        mIvTitleBarLeftToRefresh = findViewById(R.id.lwt_iv_titlebar_right_to_refresh);
        mBottomView = findViewById(R.id.lwt_tv_bottom_view);
    }

    public void initLeft(String text, OnClickListener listener) {
        mTvTitleBarLeft.setText(text);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void setLeftTextContent(String text) {
        mTvTitleBarLeft.setText(text);
    }

    public void setLeftIsVisible(boolean isVisible) {
        mTvTitleBarLeft.setVisibility(isVisible ? VISIBLE : INVISIBLE);
    }

    public void initLeft(int drawableRes, OnClickListener listener) {
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(drawableRes, 0, 0, 0);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void setLeftInvisible(boolean invisible) {
        mTvTitleBarLeft.setVisibility(invisible ? INVISIBLE : VISIBLE);
    }


    public void initLeftBack(OnClickListener listener) {
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_back_white, 0, 0, 0);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void initLeftBackBlack(OnClickListener listener) {
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_back_black, 0, 0, 0);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void initLeft(String text, int drawableRes, OnClickListener listener) {
        mTvTitleBarLeft.setText(text);
        mTvTitleBarLeft.setTextColor(Color.BLACK);
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(drawableRes, 0, 0, 0);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void setLeftContent(String text, int drawableRes) {
        mTvTitleBarLeft.setText(text);
        mTvTitleBarLeft.setTextColor(Color.BLACK);
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(drawableRes, 0, 0, 0);
    }

    public void setRightContent(String text, int drawableRes) {
        mTvTitleBarRight.setText(text);
        mTvTitleBarRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
    }

    public void initTitle(String text) {
        if (text != null)
            mTvTitleBarTitle.setText(text);
    }

    public void initTitleBlack(String text) {
        mTvTitleBarTitle.setTextColor(Color.BLACK);
        if (text != null)
            mTvTitleBarTitle.setText(text);
    }

    public void initTitle(int drawableRes) {
        mTvTitleBarTitle.setBackgroundResource(drawableRes);
    }

    public void initRight(String text, OnClickListener listener) {
        mTvTitleBarRight.setText(text);
        mTvTitleBarRight.setOnClickListener(listener);
    }

    public void initRightBlack(String text, OnClickListener listener) {
        mTvTitleBarRight.setText(text);
        mTvTitleBarRight.setTextColor(Color.BLACK);
        mTvTitleBarRight.setOnClickListener(listener);
    }

    public void initRight(int drawableRes, OnClickListener listener) {
        mTvTitleBarRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
        mTvTitleBarRight.setOnClickListener(listener);
    }

    public void initRight(String text, int drawableRes, OnClickListener listener) {
        mTvTitleBarRight.setText(text);
        mTvTitleBarRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
        mTvTitleBarRight.setOnClickListener(listener);
    }

    public void setRightBtnRes(int drawableRes) {
        mTvTitleBarRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
    }

    public void setBottomViewVisiable(boolean visiable) {
        mBottomView.setVisibility(visiable ? VISIBLE : INVISIBLE);
    }

    public void setRightImageViewRes(String url, OnClickListener listener) {
        setRightContent("", 0);
        FRImageLoad.loadImage(url, mIvTitleBarRight);
        mIvTitleBarRight.setOnClickListener(listener);
    }

    public void setRightImageViewLeftToRefresh(String url, OnClickListener listener) {
        FRImageLoad.loadImage(url, mIvTitleBarLeftToRefresh);
        mIvTitleBarLeftToRefresh.setOnClickListener(listener);
    }

    public void removeRightImageViewLeftToRefreshBg() {
        FRImageLoad.loadImage("", mIvTitleBarLeftToRefresh);
        mIvTitleBarLeftToRefresh.setOnClickListener(null);
    }
}